﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using Microsoft.Live;
using System.Text.RegularExpressions;

namespace G.SkyDrive
{
    public static class API
    {

        //private const string FoldersFilter = "{\r   \"data\": (.+?)\r}";
        //public static LiveConnectSession Session { get; set; }
        //private LiveConnectClient Client;
        //public static API()
        //{

        //}
        //public static void GetFolders(Action<Domain.Folder[]> callback, string path)
        //{
        //    string fPath = path;
        //    Microsoft.Live.LiveConnectClient client = new Microsoft.Live.LiveConnectClient(Session);
        //    client.GetCompleted += (s, e) =>
        //    {
        //        if (e.Error == null)
        //        {
        //            var match = Regex.Match(e.RawResult, FoldersFilter, RegexOptions.CultureInvariant);
        //            var str = match.Groups[1].Value;
        //            var aa = G.SkyDrive.API.Parser<G.SkyDrive.Domain.Folder[]>(match.Groups[1].Value);
        //            callback(aa);
        //        }
        //    };
        //    if (string.IsNullOrEmpty(fPath))
        //    {
        //        fPath = "me/skydrive";
        //    }
        //    fPath = string.Format("{0}/files", fPath);
        //    client.GetAsync(fPath);
        //}

        public static T Parser<T>(string json) where T : class
        {
            DataContractJsonSerializer jsonserializser = new DataContractJsonSerializer(typeof(T));
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
            {
                return jsonserializser.ReadObject(stream) as T;
            }
        }

    }
}
